package Operation;

import Book.Book;
import Book.BookList;
import jdk.dynalink.Operation;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-06-02
 * Time: 21:38
 */
public class AddOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("2. 新增图书");

        int sz = bookList.getBookSize();

        if(sz == bookList.getBooks().length) {
            System.out.println("书架满了，放不下了！");
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要新增的图书的名字：");
        String name = scanner.nextLine();

        for (int i = 0; i < sz; i++) {
            if(bookList.getBook(i).getName().equals(name)) {
                System.out.println("在书架上已经有这本书了，确定要添加吗？");
                System.out.println("是  否");
                String elect = scanner.nextLine();
                if(elect.equals("是")) {
                    break;
                } else {
                    System.out.println("好的！");
                    return;
                }
            }
        }

        System.out.println("请输入该图书的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入该图书的类型：");
        String type = scanner.nextLine();
        System.out.println("请输入该图书的价格：");
        double price = scanner.nextDouble();

        Book book = new Book(name,author,type,price);

        bookList.setBooks(sz,book);
        bookList.setBookSize(sz + 1);

        System.out.println("添加成功！");
    }
}
